<?php

/**
 * 每天定时计算 品牌榜单商品占比 算出榜单品牌指数
 * 每天4点
 */

ini_set('display_errors', 1);
ini_set('memory_limit', '1024M');
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\cache\brand\BrandRankingWikiScale as cBrandRankingWikiScale;
use Biz\cache\brand\BrandScoreCache;
use Biz\model\wiki\WikiBasic as mWikiBasic;
use Biz\model\wiki\WikiRankingCategory as mWikiRankingCategory;
use Biz\model\wiki\WikiRankingConfig as mWikiRankingConfig;
use Doraemon\cache\Wiki\SearchBrandScore;

$wikiRankingList = mWikiRankingConfig::selectByWhere(['deep' => 2, 'config_state' => mWikiRankingConfig::CONFIG_STATE_SHOW]);
$wikiRankingList = array_column($wikiRankingList, null, 'id');
$ids = array_keys($wikiRankingList);
$rankingCategorylist = mWikiRankingCategory::selectByWhere(['ranking_id' => $ids, 'cate_state' => mWikiRankingCategory::CAT_STATE_NORMAL]);
$cateIdsForRanking = [];
foreach ($rankingCategorylist as $row) {
    $cateIdsForRanking[$row['cate_id']][] = $wikiRankingList[$row['ranking_id']];
}

$where = [
    'basic_state' => mWikiBasic::STATE_ONLINE,
    'brand_id[>]' => 0,
];
$basicList = mWikiBasic::selectByWhere($where);
$basicBrandWikiList = [];
foreach ($basicList as $row) {
    $basicBrandWikiList[$row['brand_id']][] = $row;
}
$brandRankingBasicList = [];
foreach ($basicBrandWikiList as $brandId => $rowList) {
    foreach ($rowList as $basicRow) {
        if ($basicRow['category_id'] && isset($cateIdsForRanking[$basicRow['category_id']])) {
            foreach ($cateIdsForRanking[$basicRow['category_id']] as $rankingRow) {
                $brandRankingBasicList[$brandId][$rankingRow['id']][] = $basicRow['id'];
            }
        } else if ($basicRow['second_category_id'] && isset($cateIdsForRanking[$basicRow['second_category_id']])) {
            foreach ($cateIdsForRanking[$basicRow['second_category_id']] as $rankingRow) {
                $brandRankingBasicList[$brandId][$rankingRow['id']][] = $basicRow['id'];
            }
        } else if ($basicRow['third_category_id'] && isset($cateIdsForRanking[$basicRow['third_category_id']])) {
            foreach ($cateIdsForRanking[$basicRow['third_category_id']] as $rankingRow) {
                $brandRankingBasicList[$brandId][$rankingRow['id']][] = $basicRow['id'];
            }
        } else if ($basicRow['four_category_id'] && isset($cateIdsForRanking[$basicRow['four_category_id']])) {
            foreach ($cateIdsForRanking[$basicRow['four_category_id']] as $rankingRow) {
                $brandRankingBasicList[$brandId][$rankingRow['id']][] = $basicRow['id'];
            }
        }
    }
}

SearchBrandScore::delItem(SearchBrandScore::RANKING_BRAND_SCORE);
$result = [];
foreach ($brandRankingBasicList as $brandId => $rows) {
    $brandWikiCount = count($basicBrandWikiList[$brandId]);
    $brandScore = BrandScoreCache::getBrandScore($brandId);
    $brandScore = empty($brandScore) ? 1 : $brandScore;
    var_dump('brand Id:' . $brandId . '的品牌指数：' . $brandScore . '#商品数：' . $brandWikiCount);
    foreach ($rows as $rankingId => $basicIds) {
        $rankingName = isset($wikiRankingList[$rankingId]) ? $wikiRankingList[$rankingId]['ranking_name'] : '';
        $basicIds = array_unique(array_filter($basicIds));
        $countBasicIds = count($basicIds);
        $scale = ($countBasicIds / $brandWikiCount);//商品占比
        var_dump('##品牌id:' . $brandId . '#「' . $rankingName . '」榜的商品数：' . $countBasicIds . '商品占比：' . $scale);
        cBrandRankingWikiScale::setRankingWikiScale($brandId, $rankingId, $scale);
        if ($rankingName) {
            $scale = $brandScore * (1 + $scale);
            var_dump('>>品牌id:' . $brandId . '#「' . $rankingName . '」榜的品牌指数：' . $scale);
            SearchBrandScore::setRankingBrandScoreById($brandId . '_' . $rankingName, $scale);
        }
    }
}

